<?php
/**
 * Controller_Front
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-02-24 00:15:17
 * 修改记录: 
 * 
 * $Id: Front.php 2 2011-03-08 01:31:19Z coosuu@gmail.com $
 */

class Controller_Front {

    private static $_instance = null;

    private $_request;

    private $_response;

    private $_route;

    public function __construct() {

        $this->_request = Controller_Request::getInstance();
        $this->_response = Controller_Response::getInstance();
        $this->_route = Controller_Route::getInstance();
        $this->_view = new View();
    }

    public static function &getInstance() {

        if (!self::$_instance) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function dispatch() {

        $className = $this->_route->getControllerName();
        $methodName = $this->_route->getActionName();

        if (!class_exists($className)) {
            throw new Controller_Exception('Controller:' . $className . ' not exists.');
        }

        $obj = new $className($this);
        if (!method_exists($obj, $methodName)) {
            throw new Controller_Exception('Action:' . $methodName . ' not exists.');
        }

        $obj->execute($methodName);

        $this->_response->sendResponse();
    }

    public function getRequest() {

        return $this->_request;
    }

    public function getResponse() {

        return $this->_response;
    }

    public function getView() {

        return $this->_view;
    }
}
